﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DAL;

namespace UI
{
    public partial class quanlytaikhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
                // get TenDN from Session
                string tendn = Convert.ToString(Session["TenDN"]);

                // display UserName
                lbusername.Text = tendn;

                //put @param tendn into Sp(p_tendn)  
                SqlParameter[] thamso = new SqlParameter[]
            {
                new SqlParameter("@UserName",tendn),   
            };

                // Get datatable Users by TenDN
                DataTable dt = DBConnection.GetDatatableByStoreProcedureName("sp_getAllKhachHangbyUserName", thamso);

                // Display data into Screen
                txtdiachi.Text = dt.Rows[0]["DiaChi"].ToString();
                txthoten.Text = dt.Rows[0]["HoTen"].ToString();
                txtsdt.Text = dt.Rows[0]["SDT"].ToString();
                txtemail.Text = dt.Rows[0]["Email"].ToString();
                txtngaysinh.Text = dt.Rows[0]["NgaySinh"].ToString();
            }
        }

        protected void btnlaymatkhau_Click(object sender, EventArgs e)
        {
            string tendn = lbusername.Text;
            string hoten = txthoten.Text;
            string diachi = txtdiachi.Text;
            int sodt = int.Parse(txtsdt.Text);

            SqlParameter[] thamso = new SqlParameter[]
            {
                new SqlParameter("@UserName",lbusername.Text),
                new SqlParameter("@DiaChi",txtdiachi.Text),
                new SqlParameter("@Hoten",txthoten.Text),
                new SqlParameter("@SDT",txtsdt.Text),
                new SqlParameter("@Email",txtemail.Text),
                new SqlParameter("NgaySinh",DateTime.Parse(txtngaysinh.Text)),
            };
            DBConnection.QueryByStoreProcedureName("updateKhachHang", thamso);
            Response.Redirect("thongbaoloi.aspx?ID=6");
        }

        protected void bthuy_Click(object sender, EventArgs e)
        {
            //if user click Huy button, redirect to TrangChu Page.
            Response.Redirect("index.aspx");
        }

        protected void btdoimatkhau_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            lbloidoimatkhau.Visible = false;
        }

        protected void btndoimatkhau_Click(object sender, EventArgs e)
        {
            


            string matkhaumoi = DBConnection.mahoamk(txtmatkhaumoi.Text, true);
            string matkhaucu = DBConnection.mahoamk(txtmatkhaucu.Text, true);
            string tendn = Convert.ToString(Session["TenDN"]);
            SqlParameter[] thamso = new SqlParameter[]
            {
                new SqlParameter("@UserName", tendn),
                new SqlParameter("@Password",matkhaucu),
            };

            // Get User using TenDN and Password
            DataTable dt = DBConnection.GetDatatableByStoreProcedureName("sp_getAllKhachHangbyUserNameAndPassword", thamso);
            // if correct both TenDN and MatKhauDN.
            if (dt.Rows.Count > 0)
            {

                SqlParameter[] thamso1 = new SqlParameter[]
                {
                    new SqlParameter("@UserName",matkhaumoi),
                    new SqlParameter("@Password",tendn),
                };

                // Update new password.
                DBConnection.QueryByStoreProcedureName("updatePassKhachHang", thamso1);

                // redirect to Thongbao page and display congratulation message.
                Response.Redirect("thongbaoloi.aspx?ID=6");
            }
            else
            {
                lbloidoimatkhau.Visible = true;
                lbloidoimatkhau.Text = "Mật khẩu cũ của bạn không đúng, xin hay nhập lại";

            }
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}